package pira.parser.http;

import java.util.ArrayList;

public class PaperCitation extends PageParser {

	private ArrayList<Article> references;
	
	public PaperCitation(String paperURL) {
		super();
		this.url = paperURL;
		this.url = generateURL();
//System.out.println("URL for PaperCitation is : " + url);
		this.references = new ArrayList<Article>();
		try {
			this.readContent(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ArrayList<Article> getReferences() {
		return references;
	}
	
	@Override
	protected String generateURL() {
		return PREFIX + url;
	}

	@Override
	protected boolean parseResult() {
		String[] results = page.split("<tr>");
//System.out.println(results.length);
//System.out.println(results[results.length-1]);
		int index = results[results.length-1].indexOf("</table>");
		results[results.length-1] = results[results.length-1].substring(0, index);
		String[] strs = null;
		String title = null;
		String href = null;
		String[] authors = null;
		String sYear = "yyyy";
		int year = -1;
		for (int i = 1; i < results.length; i++) {
			year = -1;
			sYear = "yyyy";
			
			// get the title of the paper
			strs = results[i].split("<td>");
			index = strs[2].indexOf("</a>");
			title = strs[2].substring(0, index);
			title = title.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(
					"<[^>]*>", "");
			title = title.replaceAll("[(/>)<]", "");
			title = title.trim().replaceAll(",", "");
			
			// get the href for the paper
			int refIndex = strs[2].indexOf("href=\"");
			int endIndex = strs[2].indexOf("\">");
			href = strs[2].substring(refIndex, endIndex);
			String[] substrings = href.split("cid=");
			endIndex = substrings[0].indexOf(";jsessionid=");
			String prefix = substrings[0].substring(7, endIndex);
			href = prefix + "?cid=" + substrings[1];
//System.out.println(href);
			
			strs[2] = strs[2].substring(index);

			// get its authors
			index = strs[2].indexOf(NDASH);
			endIndex = strs[2].indexOf(HYPHEN);
			if (endIndex == -1) {
				endIndex = strs[2].indexOf("</td>");
				sYear = "0000";
			}
			
			String author = strs[2].substring(index, endIndex);
			author = author.split(NDASH)[1].trim();
			authors= author.split(", ");
			if (authors[authors.length-1].equals("et al.")) {
				if (prefix.equals(SHOWCITING)) {
					CiteURL cu = new CiteURL(href);
					String[] names = cu.getSelfAuthors();
					if (names != null)
						authors = names;
					else {
						String[] tmp = new String[authors.length - 1];
						System.arraycopy(authors, 0, tmp, 0, authors.length-1);
						authors = tmp;
					}
				} else if (prefix.equals(PAPERPREFIX)) {
					PaperCitation pc = new PaperCitation(href);
					String[] names = pc.getSelfAuthors();
					if (names != null)
						authors = names;
					else {
						String[] tmp = new String[authors.length - 1];
						System.arraycopy(authors, 0, tmp, 0, authors.length-1);
						authors = tmp;
					}
				} else {
					System.err.println("Error.");
				}
			}
				
		
			// get the publish year
			if (!sYear.equals("0000")) {
				sYear = strs[2].substring(endIndex);
//System.out.println(sYear);
				sYear = sYear.split(HYPHEN)[1].trim().substring(0, 4);
				try {
					year = Integer.parseInt(sYear);
				} catch (ClassCastException e) {
					year = -1;
					e.printStackTrace();
				}
			}
			
			// add to articles
			references.add(new Article(title, authors, year));
//System.out.println(title);
//for (String a : authors)
//	System.out.println(a);
//System.out.println(year);
//System.out.println();
		}
		return false;
	}

	@Override
	protected void parse() {
		int index = -1;
		if ((index = page.indexOf(CITELIST)) != -1) {
			page = page.substring(index);
// System.out.println(page);
			parseResult();
		} else
			System.out.println("There's no reference for this file.");
	}

	public String[] getSelfAuthors() {
		String[] authors= null;
		int index = page.indexOf(AUTHORBAR);
		if (index == -1)
			return null;
		String s = page.substring(index);
		index = s.indexOf(AUTHORBAR2);
		s = s.substring(index);
		index = s.indexOf("</div>");
		s = s.substring(0, index);
		
		s = s.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(
				"<[^>]*>", "");
		s = s.replaceAll("[(/>)<]", "");
		//s = s.trim().replaceAll(",", "");
//System.out.println(s);
		authors = s.split(",");
		for (int i = 0; i < authors.length; i++) {
			authors[i] = authors[i].trim();
			if (i == 0) {
				authors[i] = authors[i].substring(3);
				authors[i] = authors[i].trim();
			}
//System.out.println(authors[i]);
		}
		return authors;
	}
}
